package cn.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DbUntil {
	static Properties p = new Properties();
	static {
		try {
			p.load(DbUntil.class.getClassLoader().getResourceAsStream(
					"jdbc.properties"));
			Class.forName(p.getProperty("driver"));
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	private DbUntil() {

	}

	final static String url = p.getProperty("url");

	public static Connection getConnection() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, p.getProperty("user"),
					p.getProperty("password"));
			return con;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(Connection con, Statement st, ResultSet rs) {
		close(con);
		close(st);
		close(rs);
	}

	public static void close(ResultSet rs) {
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void close(Statement st) {
		try {
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void close(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(PreparedStatement pst) {
		try {
			pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
